# vim:sw=4:ts=4:expandtab

from parser import Parser, ParseError
from utils import TZInfo
import datetime

def parse_date(str):
    p = Parser(str)
    date = p.parse_date_string()
    if date is None:
        raise p.error()
    return datetime.date(*date)

def parse_time(str):
    p = Parser(str)
    time = p.parse_time_string()
    if time is None:
        raise p.error()
    return datetime.time(*time)

def parse_local_datetime(str):
    p = Parser(str)
    dt = p.parse_local_datetime_string()
    if dt is None:
        raise p.error()
    return datetime.datetime(*dt)

def parse_global_datetime(str):
    p = Parser(str)
    dt = p.parse_global_datetime_string()
    if dt is None:
        raise p.error()
    year, month, day, hour, minute, s, us, tz = dt
    tzinfo = TZInfo(*tz)
    return datetime.datetime(year, month, day, hour, minute, s, us, tzinfo)

def parse_date_or_time(str, in_content = False):
    p = Parser(str)
    dt = p.parse_date_or_time_string(in_content)
    if dt is None:
        raise p.error()
    if len(dt) == 3:
        return datetime.date(*dt)
    elif len(dt) == 4:
        return datetime.time(*dt)
    else:
        year, month, day, hour, minute, s, us, tz = dt
        tzinfo = TZInfo(*tz)
        return datetime.datetime(year, month, day, hour, minute, s, us, tzinfo)

__all__ = ['parse_date', 'parse_time', 'parse_local_datetime',
        'parse_global_datetime', 'parse_date_or_time']
